/**
 * 通用权限检查方法
 * Common check permissions method
 * @param { 权限判定 | Permission judgment } authority
 * @param { 你的权限 | Your permission description } currentAuthority
 */
export const checkPermissions = (authority, currentAuthority) => {
  // 没有判定权限.默认查看所有
  if (!authority) {
    return true
  }
  if (!currentAuthority) {
    return false
  }
  // 数组处理
  if (Array.isArray(authority)) {
    if (Array.isArray(currentAuthority)) {
      if (currentAuthority.some((item) => authority.includes(item))) {
        return true
      }
    } else if (authority.includes(currentAuthority)) {
      return true
    }
    return false
  }
  // string 处理
  if (typeof authority === 'string') {
    if (Array.isArray(currentAuthority)) {
      if (currentAuthority.some((item) => authority === item)) {
        return true
      }
    } else if (authority === currentAuthority) {
      return true
    }
    return false
  }
  return false
}

/**
 * 检测路由权限
 * @author 赵京涛
 * @param {*} originalObj 原始路由对象
 * @param {*} targetArray 目标数组
 * @returns
 */
export const filterChildrenByMenuName = (originalObj, targetArray) => {
  if (originalObj) {
    const _targetArray = targetArray.find(item => item.menuName === originalObj.label)
    return originalObj?.children?.filter(child => {
      return _targetArray?.list.some(item => item.menuName === child.label)
    })
  }
}
